#include <bits/stdc++.h>
// 2025/02/22
// tag: 
using namespace std;

#define int long long

struct point{
    int x, y;
    bool operator<(const point &p) const {
        return y < p.y;
    }
    
};

int t, n, x1;
multiset<int> k1, k2;
vector<point> l, ru, rd;



void init(){
    k1.clear(), k2.clear(), l.clear(), ru.clear(), rd.clear();
    cin >> n >> x1;
    int y1, y2, x2;
    for (int i = 1; i <= n; i++){
        cin >> y1 >> y2 >> x2;
        l.push_back({x1, y1});
        l.push_back({x1, y2});
        ru.push_back({x2, y2});
        rd.push_back({x2, y1});
    }
    sort(l.begin(), l.end());
    for (int i = 1; i <= 4 * n; i++){
        int s;
        cin >> s;
        if (s > 0) k1.insert(s);
        else k2.insert(s);
    }
}

bool check1(int x){
    multiset<int> k11 = k1;
    for (point &p: rd){
        int k = (p.y - x) / p.x;
        auto it = k11.upper_bound(k);
        if (it == k11.begin())      
            return false;
        it--;
        k11.erase(it);
    }
    return true;
}

bool check2(int x){
    multiset<int> k22 = k2;
    for (point &p: ru){
        int k = (p.y - x) / p.x;
        auto it = k22.lower_bound(k);
        if (it == k22.end()) 
            return false;
        k22.erase(it);
    }
    return true;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin >> t;
    while (t--) {
        init();
        if (k1.size() < n || k2.size() < n) {
            cout << "-1" << endl;
            continue;
        }
        int cnt = n;
        for (point p: l){
            if (cnt < k1.size())
                rd.push_back(p), cnt++;
            else
                ru.push_back(p);
        }
        int le = -1e18, ri = l[0].y, mi, mx;
        while (le <= ri){
            int mid = le + (ri - le) / 2;
            if (check1(mid))
                mi = mid, le = mid + 1;
            else
                ri = mid - 1;
        }
        le = l.back().y, ri = 1e18;
        while (le <= ri){
            int mid = le + (ri - le) / 2;
            if (check2(mid))
                mx = mid, ri = mid - 1;
            else
                le = mid + 1;
        }
        cout << mx - mi << endl;
    }
    return 0;
}